var util = require('../../libs/util.js');
var app = getApp();
Page({
  data: {
    name: "",
    region: [],
    CO_NAME:'',
    CO_ID:'',
    enterpriseList:[],
    _enterpriseList:[],
    isShare:false
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName + "--司机注册" });
    wx.showToast({
      title: "页面加载中\n请稍候...",
      icon: "loading",
      duration: 5000
    });
    // this.setData({
    //   driverId: e.id
    // });
    util.getData('/wechat/passport/getCompany', {}, function (res) {
      console.log(res.data.data)
      if (res.data.success) {
        that.setData({
          enterpriseList: res.data.data
        })
      }
    });
  },
  clearCompany:function(){
     this.setData({
       CO_NAME: '',
       CO_ID: ''
     })
  },
  bindRegionChange(e) {
    this.setData({
      region: e.detail.value
    })
  },
  onReady: function () {
    wx.hideToast()
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  }, 
  enterpriseInput: function(e) {
    var that = this;
    var value = e.detail.value;
    var enterpriseList = that.data.enterpriseList;
    var _enterpriseList = [];
    for (var i = 0; i < enterpriseList.length;i++){
      if (enterpriseList[i].CO_NAME.indexOf(value) > -1){
        _enterpriseList.push(enterpriseList[i]);
      }
    };
    if (value == ''){
      _enterpriseList = [];
    }
    that.setData({
      _enterpriseList: _enterpriseList
    })
  },
  selectEnterprise: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var _enterpriseList = that.data._enterpriseList;
    for (var i = 0; i < _enterpriseList.length; i++) {
      if (_enterpriseList[i].CO_ID == id) {
        that.setData({
          CO_NAME: _enterpriseList[i].CO_NAME,
          CO_ID: _enterpriseList[i].CO_ID
        })
        that.hideModal();
        return false;
      }
    };
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  submitDriverRegister: function (e) {
    var that = this;
    if (that.data.CO_ID == '') {
      wx.showModal({
        content: '没有填写所属企业，确认继续吗？',
        success(res) {
          if (res.confirm) {
            that.registerDriver();
          } else if (res.cancel) {
            
          }
        }
      });
    }else{
      that.registerDriver();
    }
  },
  registerDriver: function () {
    var that = this;
    if (that.data.name == '') {
      wx.showModal({
        content: '请输入姓名',
        confirmText: "确定",
        showCancel: false
      });
      return false;
    }
    if (that.data.region.length == 0) {
      wx.showModal({
        content: '请选择所在区域',
        confirmText: "确定",
        showCancel: false
      });
      return false;
    }
    wx.showLoading({
      title: '请稍候...',
    });
    util.postData("/wechat/passport/driverRegister", {
      name: that.data.name,
      region: that.data.region.join(','),
      CO_NAME: that.data.CO_NAME,
      CO_ID: that.data.CO_ID,
      isShare: that.data.isShare ? 1 : 0,
      user_id: app.globalData.user_id,
      phone: app.globalData.mobile,
      openid: app.globalData.openid,
      unionid: app.globalData.unionid
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
        return false;
      }
      if (res.data.success) {
        wx.showModal({
          title: '温馨提示',
          content: '恭喜你，注册成功！',
          success: function (res) {
            wx.redirectTo({
              url: '/pages/index/index'
            })
          }
        })
      } else if (res.data.success == false) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000,
          mask: true
        });
      }
    });
  }
});